export default {
  namespaced:true,
  state:() => ({
    //收货地址信息 
    address: JSON.parse(uni.getStorageSync('user') || '{}'),
    token:JSON.parse(uni.getStorageSync('token') || '""'),
    // 用户个人信息对象
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    // 重定向的 object 对象 { openType, from }
    redirectInfo: null
  }),
  mutations: {
    // 存收货地址的函数
    addAddress(state,obj) {
      state.address = obj
      this.commit('user/saveToStorage')
    },
    saveToStorage(state) {
      uni.setStorageSync('user', JSON.stringify(state.address))
    },
    // 持久化用户信息的函数
    getUserInof(state,obj) {
      state.userinfo = obj
      this.commit('user/saveUserInfoToStorage')
    },
    saveUserInfoToStorage(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
    },
    // 存token
    getToken(state,token) {
      state.token = token
      this.commit('user/saveTokeninfo')
    },
    saveTokeninfo(state) {
      uni.setStorageSync('token', JSON.stringify(state.token))
    },
    updateRedirectInfo(state,info) {
      state.redirectInfo = info
    }
  },
   getters: {
     addstr(state) {
       if (!state.address.provinceName) return ''
         // 拼接 省，市，区，详细地址 的字符串并返回给用户
         return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}